package com.palfund.glide;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.palfund.glide.util.GlideRoundTransform;
import com.palfund.glide.util.RoundImageView;

public class MainActivity extends AppCompatActivity {
    private static final String URL = "https://www.baidu.com/img/bdlogo.png";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //自定义圆形、圆角或椭圆
        RoundImageView ivRound = (RoundImageView) findViewById(R.id.ivRound);
        ivRound.setType(2);
        ivRound.setRoundBorderRadius(30);
        Glide.with(this).load(URL).into(ivRound);
        //分解
        ImageView ivDiv = (ImageView) findViewById(R.id.ivDiv);
        RequestOptions requestOptions = new RequestOptions();
        // 缩放模式
        requestOptions
                // 占位图
                .placeholder(R.mipmap.ic_launcher)
                // 加载失败图
                .error(R.mipmap.ic_launcher)
                // 当url为空时，回调显示的图片
                .fallback(R.mipmap.ic_launcher)
                // 修改ImageView尺寸Target.SIZE_ORIGINAL原始大小
                //.override(100, 100)
                // 加载的优先级
                .priority(Priority.HIGH)
                // 磁盘缓存策略
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                // 跳过内存缓存
                .skipMemoryCache(true)
                //缩放模式
                .centerCrop()
                // 不转换
                //.dontTransform()
                // 实现圆图、圆角图
                .transform(new GlideRoundTransform(30));
        // 不加载动画
        //requestOptions.dontAnimate();
        RequestManager requestManager = Glide.with(this);
        RequestBuilder<Drawable> requestBuilder = requestManager.asDrawable();
        requestBuilder.load(URL)
                // 缩略图
                .thumbnail(Glide.with(this).load(""))
                // 加载监听器
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model,
                                                Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model,
                                                   Target<Drawable> target, DataSource
                                                           dataSource, boolean isFirstResource) {
                        return false;
                    }
                })

                // 动画
                //requestBuilder.transition();
                // request选项
                .apply(requestOptions).into(ivDiv);
        // 实现圆图
        //requestBuilder.apply(new RequestOptions().transform(new GlideCircleTransform()));
        //requestBuilder.apply(new RequestOptions().circleCrop());
        //requestBuilder.apply(RequestOptions.circleCropTransform());
        ImageView ivNormal = (ImageView) findViewById(R.id.ivNormal);
        GlideApp.with(this).load(URL).miniThumb(0).into(ivNormal);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 1, 1, "clearMemory");
        menu.add(Menu.NONE, 2, 1, "clearDiskCache");
        menu.add(Menu.NONE, 3, 1, "Jump");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                // 清除内存缓存
                GlideApp.get(this).clearMemory();
                break;
            case 2:
                // 清除磁盘缓存,必须在工作线程
                new Thread() {
                    @Override
                    public void run() {
                        GlideApp.get(MainActivity.this).clearDiskCache();
                    }
                }.start();
                break;
            case 3:
                startActivity(new Intent(this, RVActivity.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
